package com.vision.sales.deliveryreceipt.cl.ui;

import static com.vision.sales.cl.SalesServices.deliveryReceiptSvc;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.form.validator.MaxLengthValidator;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.ui.UI;
import com.vision.sales.cl.SalesServices;
import com.vision.sales.cl.event.DeliveryReceiptCreatedEvent;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cl.ui.component.CustomerComboBox;
import com.vision.sales.cm.db.data.Customer;
import com.vision.sales.cm.db.data.DeliveryReceipt;
import com.vision.sales.cm.db.data.DeliveryReceipt.FieldSpecs;

/**
 * 
 * @author Mark
 *
 */
public class CreateDeliveryReceiptWindow implements IsWidget, Editor<DeliveryReceipt> {
	
	interface Binder extends UiBinder<Widget, CreateDeliveryReceiptWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);

	// Form fields that are bound by the form driver to the fields in the DeliveryReceipt class.
	// The name of these form fields must match the name of the field in the DeliveryReceipt class.
	@UiField TextField drNo;
	@UiField DateField date;
	@UiField CustomerComboBox customer;
	@UiField TextArea remarks;
	
	// Ignored by the form driver since these are not fields in the DeliveryReceipt
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;
	
	@UiField Window window;
	
	interface ProductDriver extends SimpleBeanEditorDriver<DeliveryReceipt, CreateDeliveryReceiptWindow> { }
	
	private ProductDriver driver = GWT.create(ProductDriver.class); // Used to bind the grid to the form editor
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(SalesResources.INSTANCE.deliveryReceipts());
		
		loadData();
		
		driver.initialize(this);
		driver.edit(new DeliveryReceipt(new Timestamp(System.currentTimeMillis())));
		
		drNo.addValidator(new MaxLengthValidator(FieldSpecs.DR_NO_MAX));
		remarks.addValidator(new MaxLengthValidator(FieldSpecs.REMARKS_MAX));
		
		date.setPropertyEditor(UI.datePropertyEditor());
		date.setValue(new Date());
		
		return window;
	}
	
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	private void loadData() {
		customer.mask("Loading...");
		SalesServices.customerSvc().getCustomerNameAndIdOnly(new Callback<List<Customer>>(customer) {
			@Override
			protected void handleSuccess(List<Customer> result) {
				customer.getStore().clear();
				customer.getStore().addAll(result);
			}
		});
	}
	
	// UiHandlers %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		DeliveryReceipt deliveryReceipt = driver.flush();
		if (driver.hasErrors())
			return;
		
		UI.mask(window, "Creating delivery receipt...");
		deliveryReceiptSvc().create(deliveryReceipt, new Callback<DeliveryReceipt>(window) {
			@Override
			protected void handleSuccess(DeliveryReceipt result) {
				window.hide(saveBtn);
				
				BaseModule.fireEvent(new DeliveryReceiptCreatedEvent(result));
			}
		});
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		window.hide(cancelBtn);
	}

}
